package com.lanou.order.service.impl;



import com.lanou.entity.entity.CartItem;
import com.lanou.entity.entity.OrderEntity;
import com.lanou.entity.entity.OrderItemEntity;
import com.lanou.entity.entity.OrderShippingEntity;
import com.lanou.entity.utils.AjaxResult;
import com.lanou.order.mapper.OrderMapper;
import com.lanou.order.service.OrderService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public List<OrderEntity> query(int page, String submitDate, String orderState, String keyWord) {

        Calendar curr = Calendar.getInstance();
        int index = (page - 1) * 5;

        // 判断后的数据
        String orderState1 = this.getOrderState(orderState);
        int submitDate1 = this.getSubmitDate(submitDate);

        List<OrderEntity> orderEntityList = this.orderMapper.queryBy(index, submitDate1, orderState1, keyWord);
        List<OrderItemEntity> orderItemEntityList = this.orderMapper.queryItem(keyWord);

        for (OrderEntity orderEntity : orderEntityList) {
            for (OrderItemEntity orderItemEntity : orderItemEntityList) {
                if (orderEntity.getOrderId().equals(orderItemEntity.getOrderId())) {
                    orderEntity.getOrderItemList().add(orderItemEntity);
                }
            }
        }

        return orderEntityList;
    }

    @Override
    public List<OrderEntity> queryAll(int page) {

        return this.orderMapper.queryAll((page - 1) * 5);
    }

    @Override
    public List<Integer> queryState() {
        Integer countOne = this.orderMapper.countOne();
        Integer countFour = this.orderMapper.countFour();
        ArrayList<Integer> list = new ArrayList<>();
        list.add(countOne);
        list.add(countFour);
        return list;
    }

    @Override
    public int queryCount() {

        int page = 0;
        if (this.orderMapper.queryCount() % 5 != 0) {
            page = this.orderMapper.queryCount() / 5 + 1;
        }else {
            page = this.orderMapper.queryCount() / 5;
        }

        return page;
    }

    @Override
    public void update(String orderId, String status) {
        String sta = null;
        if (status.equals("1")){
            sta = "2";
        }
        if (status.equals("2") || status.equals("3") || status.equals("4")){
            sta = "5";
        }
        this.orderMapper.update(orderId, sta);

    }

    @Override
    public AjaxResult insert(OrderShippingEntity orderShippingEntity,
                             String payType,
                             String userOrderCartId,
                             String total) {

        // 订单
        OrderEntity orderEntity = new OrderEntity();
        int orderId = (int) (Math.random() * 100000 + 1000);

        orderEntity.setOrderId(String.valueOf(orderId));
        orderEntity.setCreateTime(new Date());
        orderEntity.setUserId(1001L);
        orderEntity.setPayment(total);
        orderEntity.setStatus(1);
        orderEntity.setPaymentType(Integer.valueOf(payType));
        int result = this.orderMapper.insertOrder(orderEntity);


        // 订单项
        ArrayList<OrderItemEntity> orderItemList = new ArrayList<>();
        ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
        List<CartItem> list = (List<CartItem>) ops.get(userOrderCartId);

        for (CartItem cartItem : list) {
            OrderItemEntity orderItemEntity = new OrderItemEntity();
            orderItemEntity.setId(String.valueOf(Math.random() * 100000 + 1000));
            orderItemEntity.setNum(cartItem.getCount());
            orderItemEntity.setOrderId(String.valueOf(orderId));
            orderItemEntity.setItemId(String.valueOf(cartItem.getEntity().getId()));
            orderItemEntity.setTitle(cartItem.getEntity().getTitle());
            orderItemList.add(orderItemEntity);
        }
        int result1 = this.orderMapper.insertOrderItem(orderItemList);


        // 收货人信息
        orderShippingEntity.setOrderId(String.valueOf(orderId));
        int result2 = this.orderMapper.insertOrderShipping(orderShippingEntity);

        if (result > 0 && result1 > 0 && result2 > 0){
            AjaxResult ar = AjaxResult.builder().total(1)
                    .data(orderId)
                    .build();
            return ar;
        }else {
            AjaxResult ar = AjaxResult.builder().total(0)
                    .build();
            return ar;
        }





    }

    @Override
    public void delete(int orderId) {
        this.orderMapper.delete(orderId);
    }


//    public void insert(OrderEntity orderEntity, OrderShippingEntity orderShippingEntity, List<OrderItemEntity> orderItemEntityList) {
//
//
//        // 订单项
//        String userId = "userID:7";
//        ValueOperations<String, Object> ops = this.redisTemplate.opsForValue();
//        List cartItems = (List) ops.get(userId);
//        System.out.println("----" + cartItems);
//
////        this.orderMapper.insertOrderItem();
////
////        // 收货人信息
////        this.orderMapper.insertOrderShipping();
//    }


//    public void insert(OrderShippingEntity orderShippingEntity, String payType, String total) {
//        // 订单
//        OrderEntity orderEntity = new OrderEntity();
//        int orderId = (int)Math.random() * 100000 + 1000;
//        orderEntity.setOrderId(String.valueOf(orderId));
//        orderEntity.setCreateTime(new Date());
//        orderEntity.setUserId(1001L);
//        orderEntity.setPaymentType(Integer.valueOf(payType));
//        orderEntity.setPayment(total);
//        this.orderMapper.insertOrder(orderEntity);
//    }


    public int getSubmitDate(String submitDate) {
        //   1 最近三个月          4096 所有状态
        //   2 今年内             1     等待付款()
        //   2018                 3     等待自提(已付款)
        //   2017                 128   等待收货
        //   2016                 1024   已完成
        //   3 之前               -1      已取消

//        状态：1、未付款，2、已付款， 3、(暂砍掉)未发货 ，4、已发货，5、交易成功，6、交易关闭
        Calendar cal = Calendar.getInstance();

        if ("1".equals(submitDate)) {
            return cal.get(Calendar.MONTH) + 1;
        } else if ("2".equals(submitDate)) {
            return cal.get(Calendar.YEAR);
        } else if ("2018".equals(submitDate)) {
            return cal.get(Calendar.YEAR) - 1;
        } else if ("2017".equals(submitDate)) {
            return cal.get(Calendar.YEAR) - 2;
        } else if ("2016".equals(submitDate)) {
            return cal.get(Calendar.YEAR) - 3;
        } else if ("3".equals(submitDate)) {
            return cal.get(Calendar.YEAR) - 3 + 1000;
        }

        return 0;
    }

    public String getOrderState(String orderState) {

        if ("4096".equals(orderState)) {
            return " ";
        } else if ("1".equals(orderState)) {
            return "1";
        } else if ("32".equals(orderState)) {
            return "2";
        } else if ("128".equals(orderState)) {
            return "4";
        } else if ("1024".equals(orderState)) {
            return "5";
        } else if ("-1".equals(orderState)) {
            return "6";
        }

        return null;
    }



}
